﻿using Minio;
using RuYiAdmin.Net.UniversalFoundation.Configurations;
using System;

namespace RuYiAdmin.Net.UniversalFoundation.Contexts
{
    /// <summary>
    /// Minio会话单例
    /// </summary>
    public static class RuYiMinioContext
    {
        /// <summary>
        /// Lazy对象
        /// </summary>
        private static readonly Lazy<MinioClient> LazyInstance = new Lazy<MinioClient>(() =>
        {
            if (RuYiGlobalConfig.MinioConfig.IsEnabled)
            {
                var minioClient = new MinioClient()
                .WithEndpoint(RuYiGlobalConfig.MinioConfig.Endpoint)
                .WithCredentials(RuYiGlobalConfig.MinioConfig.AccessKey, RuYiGlobalConfig.MinioConfig.SecretKey)
                .Build();
                return minioClient as MinioClient;
            }
            else
            {
                throw new InvalidOperationException("minio is not enabled in the configuration.");
            }
        });

        /// <summary>
        /// 单例对象
        /// </summary>
        public static MinioClient Instance { get { return LazyInstance.Value; } }

        /// <summary>
        /// 是否已创建
        /// </summary>
        public static bool IsInstanceCreated { get { return LazyInstance.IsValueCreated; } }
    }
}
